# !/usr/bin/env python
# encoding: utf-8
# @File    : producer_table_model_module.py
# @Time    : 2021/2/1
# @Author  : qiaowei
# @Version : 1.0
# @Email   : weiweiqiao@126.com
# @License : (C) Copyright 2013-2021
# @Brief   :


import PySide2.QtCore
import typing
from PySide2.QtCore import Qt
from PySide2.QtCore import QAbstractTableModel
from PySide2.QtGui import QFont
from PySide2.QtGui import QColor
from multithread.track import Track


class ProducerTableModel(QAbstractTableModel):
    def __init__(self, buffer_list: list = None, parent: PySide2.QtCore.QObject = None):
        super(ProducerTableModel, self).__init__(parent)

        # 设置保存用户数据项列表
        if buffer_list is not None:
            self.__buffer_list = buffer_list
        else:
            self.__buffer_list = []

    def columnCount(self, parent: PySide2.QtCore.QModelIndex = ...) -> int:
        """
        返回模型的列数，返回2列

        Args:
            parent: 数据项的索引

        Returns:
            2列

        """
        return 2

    def rowCount(self, parent: PySide2.QtCore.QModelIndex = ...) -> int:
        """
        返回模型的行数，当模型的数据集为None时，返回0；当数据集有数据时，返回数据个数（行数）

        Args:
            parent: 数据项的索引

        Returns:
            模型数据的个数

        """
        if self.__buffer_list is None:
            return 0
        else:
            return len(self.__buffer_list)

    def data(self, index: PySide2.QtCore.QModelIndex, role: int = ...) -> typing.Any:
        """
        根据索引index，角色role返回对应位置处的数据，数据可以是任何类型

        Args:
            index: 数据项索引
            role: 数据项的角色

        Returns:
            根据索引、角色对应的数据，可能是显示数据，也可能是表格的背景色

        """
        var = False

        if Qt.DisplayRole == role:
            if self.__buffer_list is not None and 0 != len(self.__buffer_list):
                if 0 == index.column():
                    var = self.__buffer_list[index.row()].thread_id
                elif 1 == index.column():
                    var = self.__buffer_list[index.row()].char
            return var

        if Qt.BackgroundColorRole == role:
            i = index.row() % 2

            if 0 == i:
                # 天蓝色
                var = QColor(135, 206, 255)
            elif 1 == i:
                # 黄色
                var = QColor(255, 255, 0)
            return var

    def headerData(self, section: int,
                   orientation: PySide2.QtCore.Qt.Orientation,
                   role: int = ...) -> typing.Any:
        """
        设置表头

        Args:
            section: 表头序号
            orientation: 表头水平/垂直标识
            role: 表头角色

        Returns:
            返回表头设置

        """
        if Qt.Horizontal == orientation:
            if Qt.DisplayRole == role:
                if 0 == section:
                    return "线程编号"
                elif 1 == section:
                    return "数据"

            # 设置表头字体
            if Qt.FontRole == role:
                return QFont("Courier New", 10, QFont.Bold)

            # 设置表头颜色
            if Qt.TextColorRole == role:
                return QColor(255, 193, 37, 127)

        # 其它情况调用父类方法
        return super(ProducerTableModel, self).headerData(section, orientation, role)

    def add_data_to_buffer_list(self, index: int, thread_id: int, char: str):
        """
        根据传入数据，生成Track类对象，并保存到列表中

        Args:
            index: 序列号
            thread_id: 线程号
            char: 资源

        """
        self.__buffer_list.append(Track(index, thread_id, char))
        # self.emit(self, self.)
